import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_main/main_mine/state.dart';
import 'package:seedfour/toolkit/typedef/function.dart';

class MineProperty extends StatelessWidget {
  /// 数据源
  final MineState data;

  /// 点击余额卡片
  final ParamVoidCallback onClickBalance;

  /// 点击押金卡片
  final ParamVoidCallback onClickDeposit;

  final ParamVoidCallback onClickFund;

  final ParamVoidCallback onClickIncome;

  final ParamVoidCallback onClickAlloc;

  final ParamVoidCallback onClickExpense;

  const MineProperty({
    Key? key,
    required this.data,
    required this.onClickBalance,
    required this.onClickDeposit,
    required this.onClickFund,
    required this.onClickIncome,
    required this.onClickAlloc,
    required this.onClickExpense,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Ink(
      width: double.maxFinite,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(30.w),
          topRight: Radius.circular(30.w),
        ),
      ),
      child: Obx(
        () => Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.only(top: kSpaceMedium, left: kSpaceMedium),
              child: Text(
                '资产',
                style: TextStyle(
                  color: AppColor.colorFontBlack,
                  fontSize: kFontSizeMedium,
                  fontWeight: AppFont.fontMedium,
                ),
              ),
            ),
            SizedBox(height: kSpaceMini),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
              child: _buildProperty(),
            ),
            SizedBox(height: kSpaceLarge),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
              child: Text(
                '统计',
                style: TextStyle(
                  color: AppColor.colorFontBlack,
                  fontSize: kFontSizeMedium,
                  fontWeight: AppFont.fontMedium,
                ),
              ),
            ),
            SizedBox(height: kSpaceMedium),
            Column(
              children: [
                _buildStatistics(
                  '创收',
                  data.statisticsInfo.value.incomeAmount ?? '0',
                  () => onClickIncome(),
                ),
                data.role.value != 'me'
                    ? Container()
                    : _buildStatistics(
                        '收入',
                        data.statisticsInfo.value.allocAmount ?? '0',
                        () => onClickAlloc(),
                      ),
                _buildStatistics(
                  '支出',
                  data.statisticsInfo.value.expendAmount ?? '0',
                  () => onClickExpense(),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget _buildStatistics(
      String title, String value, ParamVoidCallback onClick) {
    return InkWell(
      onTap: () => onClick(),
      child: Padding(
        padding: EdgeInsets.symmetric(
            vertical: kSpaceSmall, horizontal: kSpaceMedium),
        child: Row(
          children: [
            Expanded(
              child: Text(
                title,
                style: TextStyle(
                  color: AppColor.colorFontBlack,
                  fontSize: kFontSizeSmall,
                  fontWeight: AppFont.fontRegular,
                ),
              ),
            ),
            Text(
              value,
              style: TextStyle(
                color: AppColor.colorFontBlack,
                fontSize: kFontSizeSmall,
                fontWeight: AppFont.fontRegular,
              ),
            ),
            Icon(
              Icons.arrow_forward_ios,
              color: AppColor.colorFontGrey,
              size: 13.w,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildProperty() {
    return Container(
      width: double.maxFinite,
      padding:
          EdgeInsets.symmetric(vertical: kSpaceMedium, horizontal: kSpaceLarge),
      decoration: BoxDecoration(
        gradient: const LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              Color(0xFF7D86FF),
              Color(0xFF5863FF),
            ]),
        borderRadius: BorderRadius.circular(kRadiusSmall),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          InkWell(
            onTap: () => onClickBalance(),
            child: _buildBalance(
                '余额(元)',
                data.statisticsInfo.value.userBalanceList == null
                    ? '0'
                    : data.statisticsInfo.value.userBalanceList!.balance!
                        .sumAmount!
                        .replaceAll('¥', '')),
          ),
          Container(
            width: 0.5.w,
            height: 20.w,
            color: const Color(0xFFA4A9F1),
          ),
          InkWell(
            onTap: () => onClickFund(),
            child: _buildBalance(
                '专款(元)',
                data.statisticsInfo.value.userBalanceList == null
                    ? '0'
                    : data
                        .statisticsInfo.value.userBalanceList!.alloc!.sumAmount!
                        .replaceAll('¥', '')),
          ),
          Container(
            width: 0.5.w,
            height: 20.w,
            color: const Color(0xFFA4A9F1),
          ),
          InkWell(
            onTap: () => onClickDeposit(),
            child: _buildBalance(
                '押金(元)',
                data.statisticsInfo.value.userBalanceList == null
                    ? '0'
                    : data.statisticsInfo.value.userBalanceList!.margin!
                        .sumAmount!
                        .replaceAll('¥', '')),
          ),
        ],
      ),
    );
  }

  Widget _buildBalance(String title, String value) {
    return Column(
      children: [
        Text(
          title,
          style: TextStyle(
            color: Colors.white,
            fontSize: kFontSizeMini,
            fontWeight: AppFont.fontRegular,
          ),
        ),
        SizedBox(height: kSpaceTiny),
        Text(
          value,
          style: TextStyle(
            fontSize: kFontSizeMini,
            color: Colors.white,
            fontWeight: AppFont.fontExtraBold,
          ),
        ),
      ],
    );
  }
}
